package com.trg.flex.dt
{
	import flash.events.Event;
	
	import mx.controls.DataGrid;
	import mx.controls.RadioButton;
	import mx.controls.listClasses.BaseListData;

	/**
	 * This is used internally by SelectionColumn to show a radio button in a cell.
	 */
	public class RadioButtonSelectionRenderer extends RadioButton
	{
		public function RadioButtonSelectionRenderer()
		{
			super();
			setStyle("paddingLeft", 4);
			setStyle("paddingRight", 4);
		}
		
		private var _data:*;
		private var _listData:BaseListData;
		
		override public function set listData(value:BaseListData):void {
			if (_listData != null) _listData.owner.removeEventListener("change", updateSelection);
			_listData = value;
			_listData.owner.addEventListener("change", updateSelection);
		}
		
		override public function get listData():BaseListData {
			return _listData;
		}
		
		override public function set data(value:Object):void {
			_data = value;
			updateSelection();
		}
		override public function get data():Object {
			return _data;
		}
		
		private function updateSelection(e:Event = null):void {
			selected = (DataGrid(_listData.owner).selectedItem === _data);
		}
		
		/**
		 * This will prevent the default toggle behavior of the checkbox. So basically
		 * the checkbox doesn't do anything when it is clicked.
		 */
		override public function get toggle():Boolean {
			return false;
		}
		
	}
}